﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Maticsoft.Model;
using Quartz;
using CronExpressionDescriptor;

public partial class site_mag_AddAPI : System.Web.UI.Page
{   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.QueryString["adid"] != null)
            {   
                int id = Convert.ToInt32(Request.QueryString["adid"]);
                Maticsoft.BLL.bll_api bll = new Maticsoft.BLL.bll_api();
                Maticsoft.Model.model_api model = bll.GetModel(id);
                txtapiname.Text = model.apiname;
                txtapiurl.Text = model.apiurl;
                txtapimemo.Text = model.apimemo;
                ddlhttpmethod.Text = model.httpmethod;
                ddlcontenttype.Text = model.contenttype;
                txtcontent.Text = model.content;
                ddlstatus.Text = model.status;
                cbisusing.Checked = model.isusing;

                if(!string.IsNullOrWhiteSpace(model.apimemo))
                {
                    CronExpression ce = new CronExpression(model.apimemo);
                    string summary = ce.GetExpressionSummary();
                }

            }
        }

        if (!string.IsNullOrWhiteSpace(txtapimemo.Text))
        {   
            lblMemo.Text = CronExpressionDescriptor.ExpressionDescriptor.GetDescription(txtapimemo.Text,
                    new Options()
                    {
                        Locale = "zh-CN"
                    }
                );
        }

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtapiname.Text) || string.IsNullOrEmpty(txtapiurl.Text))
        {
            MessageBox.Show(this, "名称或地址不能为空！");
            return;
        }
        
        Maticsoft.Model.model_api model = new Maticsoft.Model.model_api();

        if (Request.QueryString["adid"] == "" || Request.QueryString["adid"] == null)
        {
            model.apiname = txtapiname.Text;
            model.apiurl = txtapiurl.Text;
            model.apimemo = txtapimemo.Text;
            model.status = ddlstatus.Text;
            model.contenttype = ddlcontenttype.Text;
            model.content = txtcontent.Text;
            model.isusing = cbisusing.Checked;
            model.httpmethod = ddlhttpmethod.Text;

            Maticsoft.BLL.bll_api bll = new Maticsoft.BLL.bll_api();
            try
            {
                if (bll.Add(model))
                {
                    MessageBox.Show(this, "提交成功");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "提交出错！");
            }
        }
        else
        {
            int id = Convert.ToInt32(Request.QueryString["adid"]);

            Maticsoft.BLL.bll_api bll = new Maticsoft.BLL.bll_api();
            DataSet ds1 = bll.GetList("id=" + id);
            model.id = id;

            model.apiname = txtapiname.Text;
            model.apiurl = txtapiurl.Text;
            model.apimemo = txtapimemo.Text;
            model.status = ddlstatus.Text;
            model.contenttype = ddlcontenttype.Text;
            model.content = txtcontent.Text;
            model.isusing = cbisusing.Checked;
            model.httpmethod = ddlhttpmethod.Text;

            try
            {
                if (bll.Update(model))
                {
                    MessageBox.Show(this, "更新成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "更新出错！");
            }
        }

        CronService.Instance.Restart();
        
    }



    protected void btnBack_Click(object sender, EventArgs e)
    {   

        this.Response.Redirect("APIList.aspx");
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(ddlTimeSpans.SelectedValue))
            return;

        txtapimemo.Text = ddlTimeSpans.SelectedValue;


        lblMemo.Text = CronExpressionDescriptor.ExpressionDescriptor.GetDescription(txtapimemo.Text,
                   new Options()
                   {
                       Locale = "zh-CN"
                   }
               );

    }
}